package com.lcx.core;

import com.lcx.annotation.JsObject;
import com.lcx.util.JsTemplateUtil;
import com.lcx.util.StringUtil;

import java.lang.reflect.Method;

public class JsTemplate {

    private Object toJsObj;

    private String prefPath;

    public JsTemplate(Object toJsObj,String prefPath){
        this.toJsObj = toJsObj;
        this.prefPath = prefPath;
    }
    public JsTemplate(Object toJsObj){
        this.toJsObj = toJsObj;
    }

    public String collect(){
        String objName;
        String cPath=null;
        String mPath = null;
        String pPath = null;
        JsObject jsObject = toJsObj.getClass().getAnnotation(JsObject.class);
        if(jsObject == null){
            return null;
        }
        if(prefPath!=null){
            pPath = prefPath;
            int index;
            if((index=pPath.indexOf("?"))>-1){
                pPath = pPath.substring(0,index);
            }
            if(pPath.endsWith("/")){
                pPath = pPath.substring(0,pPath.length()-1);
            }
        }
        cPath = getJsObjectPath(jsObject);
        if(!StringUtil.isNotNull(jsObject.name())){
            objName = StringUtil.modifySCName(toJsObj.getClass().getSimpleName());
        }else{
            objName = jsObject.name();
        }
        Method method[] = toJsObj.getClass().getMethods();
        if(method != null){
            String methodName;
            Class<?> params[];
            StringBuilder jsScript = new StringBuilder();
            jsScript.append("(function(){")
                    .append("var var_= window.").append(objName)
                    .append(" = {};");
            JsObject mJsObject;
            for(Method m:method){
                params = m.getParameterTypes();
                if((mJsObject=m.getAnnotation(JsObject.class))!=null){
                    //根据地址过滤方法
                    if(pPath!=null){
                        mPath = getJsObjectPath(mJsObject);
                        if(!cPath.isEmpty() || !mPath.isEmpty()){
                            cPath=clearBothDo(cPath);
                            mPath=clearBothDo(mPath);
                            if(mPath.isEmpty()) mPath=cPath;
                            else if(cPath.isEmpty()) mPath=mPath;
                            else mPath=cPath+"/"+mPath;
                            if(!pPath.endsWith(mPath)){
                                continue;
                            }
                        }
                    }
                    if(mJsObject.sync()){
                        //为每一个方法创建一个调用方法，同步方法
                        jsScript.append(JsTemplateUtil.generateSyncProxyMethod(objName,m));
                    }else{
                        //为每一个方法创建一个调用方法，successCallback和errorCallback
                        jsScript.append(JsTemplateUtil.generateProxyMethod(objName,m));

                    }
                }
            }
            jsScript.append("})()");
            return jsScript.toString();
        }
        return null;
    }

    private String getJsObjectPath(JsObject jsObject){
        return jsObject.value()==null||jsObject.value().isEmpty()?"":jsObject.value();
    }

    private String clearBothDo(String path){
        if(path!=null){
            if(path.startsWith("/")) path = path.substring(1);
            if(path.endsWith("/")) path = path.substring(0,path.length()-1);
        }
        return path;
    }


    public Object getToJsObj() {
        return toJsObj;
    }

    public void setToJsObj(Object toJsObj) {
        this.toJsObj = toJsObj;
    }

}
